<?php
/**
 * Created by PhpStorm.
 * User: Vito
 * Date: 2022/5/19
 * Time: 15:49
 */

namespace app\admin\controller\v1;


use app\admin\controller\BaseController;
use app\logic\AttachmentLogic;
use app\service\ExcelImportService;
use app\model\WaterEleRecord;
use app\model\User;
use app\common\EventName;
use think\facade\Event;
use app\model\Room;
class Attachment extends BaseController
{
    protected $name = '附件';

    const NOT_NEED_AUTH = ['upload'];
    const NOT_NEED_LOGIN = ['upload'];
    protected $methodName = [
        'upload'                => '客户端上传附件',
        'importWaterEleExcel'   => '导入水电费记录',
        'importUserExcel'       => '导入租户',
        'importRoomExcel'       => '导入房间资料'
    ];

    // const NOT_NEED_LOGIN = ['*'];

    protected $registerCurd = true;

    protected $registerCurdDir = true;

    protected function initialize()
    {
        $this->logic = new AttachmentLogic();
    }


    /**
     * @Description: 导入水电费记录
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2023-04-18 16:36:12
     * @version: 1.0.0
     * @return [type]
     */
    public function importWaterEleExcel()
    {
        $params = $this->request->post();
        $user = $this->user->getUserinfo();
        // $user = null;
        $model =  new WaterEleRecord();
        $excel = new ExcelImportService();
        $field = [
            'housing_id' => '',
            'building_id' => '',
            'room_id' => '',
            // 'place' => '',  
            'before_water' => '',
            'water' => '',
            'before_electricity' => '',
            'electricity' => '',
            'record_date'=>'',
        ];
        $isFindField = [
            '='         =>  'room_id',
            'between'   => 'record_date'
        ];
        // halt($isFindField);
        $data = $excel->updExcel($model, $field, $isFindField, $user, 'roomGetMethod', false, true, false,'room_id');

        Event::trigger(EventName::CRONTAB_TASK);
        return success('', $data);
    }

    /**
     * @Description: 导入水电费记录
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2023-04-18 16:36:12
     * @version: 1.0.0
     * @return [type]
     */
    public function importUserExcel()
    {
        $params = $this->request->post();
        $user = $this->user->getUserinfo();
        // $user = null;
        $model =  new User();
        $excel = new ExcelImportService();
        $field = [
            'start_time'    => '',
            'lease_mark'    => '',
            'housing_id'    => '',
            'building_id'   => '',
            'room_id'       => '',
            'word_unit'     => '',
            'real_name'     => '',
            'sex'           => '',
            'tel'           => '',
            'id_card'       => '',
            'propertyPrice' => '',
            'fee'           => '',
            'room_lease_mark'=> ''
        ];
        $isFindField = [
            '='         =>  'tel',
            // 'between'   => 'record_date'
        ];
        // halt($isFindField);
        $data = $excel->updExcel($model, $field, $isFindField, $user, 'userMethod', 'userStatus', false, 'userFindMethod',false);

        // Event::trigger(EventName::CRONTAB_TASK);
        return success('', $data);
    }
   
     /**
     * @Description: 导入房间资料
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2023-04-18 16:36:12
     * @version: 1.0.0
     * @return [type]
     */
    public function importRoomExcel()
    {
        $params = $this->request->post();
        $user = $this->user->getUserinfo();
        // $user = null;
        $model =  new Room();
        $excel = new ExcelImportService();
        $field = [
            'housing_id'        => '',
            'building_number'   => '',
            // 'floor'             => '',
            'name'              => '',
            'room_type'         => '',
            'area'              => '',
            'live_number'       => '',
            // 'alone'             => '',
            'amount'            => '',
            'number'            => 0
        ];
        $isFindField = [
            '='         =>  'building_number',
            '='         =>  'name',
            // 'between'   => 'record_date'
        ];
        $data = $excel->updExcel($model, $field, $isFindField, $user, 'roomMethod', false, false,false,false);
        return success('', $data);
    }
}